<?php

namespace App\Admin\Controllers\Coupon;

use App\Admin\Actions\Post\BatchRestore;
use App\Admin\Actions\Post\Restore;
use App\Admin\Repositories\CmcPromotionCoupon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB as CmcGoods;

class CmcPromotionCouponController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CmcPromotionCoupon(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('integral');
            $grid->column('coupon_price');
            $grid->column('coupon_time');
//            $grid->column('sort');
            $grid->column('status');
            $grid->column('status', '状态')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'未开启',
                    1=>'开启'
                ])
            );
            $grid->column('add_time');
            $grid->column('id_del');
            $grid->column('goods_name','所属商品');
            $grid->column('category_name','所属分类');
            $grid->column('type');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            // 回收站
            $grid->filter(function ($filter) {
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(\App\Models\coupon\CmcPromotionCoupon::class));
                }
            });
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(\App\Models\coupon\CmcPromotionCoupon::class));
                }
            });
            // 异步渲染功能
            $grid->async();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CmcPromotionCoupon(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('integral');
            $show->field('coupon_price');
            $show->field('coupon_time');
            $show->field('sort');
            $show->field('status');
            $show->field('add_time');
            $show->field('id_del');
            $show->field('goods_name');
            $show->field('category_name');
            $show->field('type');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CmcPromotionCoupon(), function (Form $form) {
            $form->display('id');
            $form->text('title')->rules('required');
            $form->text('integral')->rules('required');
            $form->decimal('coupon_price')->rules('required');
            $form->text('coupon_time')->rules('required');
            $form->text('sort')->rules('required');
            $form->text('status')->rules('required');
            $form->datetime('add_time')->rules('required');
            $form->text('id_del')->rules('required');
            $form->select('category_name','所属分类')->options(
                CmcGoods::table('cmc_goods_category')->where('is_show',1)->pluck('category_name','category_name')
            )->rules('required');
            $form->select('goods_name','所属商品')->options(
                CmcGoods::table('cmc_goods')->where('goods_state',1)->pluck('goods_name','goods_name')
            )->rules('required');
            $form->select('type','优惠券类型')->options(
                CmcGoods::table('cmc_promotion_coupon_type')->where('status',1)->pluck('type','type')
            )->rules('required');
            $form->display('created_at');
            $form->display('updated_at');
            $form->confirm('您确定要提交表单吗？');
        });
    }
}
